﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace BusinessStorage.GeoLoc
{
    public class GeoLoc
    {
        public static KeyValuePair<String, String> FindGeoLocByIp(string IP)
        {

            string IpGeolocCall;
            KeyValuePair<String, String> Geo;
            IpGeolocCall = "http://ipinfodb.com/ip_query.php" + "?ip=" + IP + "&timezone=false";
            try
            {
                using (XmlReader readerIp = XmlReader.Create(IpGeolocCall))
                {
                    XDocument xdoc = XDocument.Load(readerIp);
                    string latit = xdoc.Descendants("Latitude").First().Value;
                    string longit = xdoc.Descendants("Longitude").First().Value;
                    Geo = new KeyValuePair<string, string>(latit, longit);
                }
            }
            catch (Exception)
            {
                Geo = new KeyValuePair<string, string>(null, null);
            }
            return Geo;
        }
        public void getBoundingBox(double latit,double longit)
        { 
            
        }
        //public static List<Place> FindNearPlaces(KeyValuePair<string, string> GeoLoc)
        //{
        //    string ApiCall;
        //    ApiCall = "http://api.foursquare.com/v1/venues?geolat=" + GeoLoc.Key + "&geolong=" + GeoLoc.Value;
        //    try
        //    {
        //        using (XmlReader readerIp = XmlReader.Create(ApiCall))
        //        {
        //            XDocument xdoc = XDocument.Load(readerIp);
        //            return (from place in xdoc.Descendants("venue")
        //                    select new Place
        //                    {
        //                        Adress = place.Element("address").Value,
        //                        City = place.Element("city").Value,
        //                        Id = place.Element("id").Value,
        //                        Lat = place.Element("geolat").Value,
        //                        Long = place.Element("geolong").Value,
        //                        Name = place.Element("name").Value,
        //                        CategoryIcon = place.Descendants("iconurl").Select(pl => pl.Value)
        //                        .DefaultIfEmpty("http://foursquare.com/img/categories/building.png").FirstOrDefault(),
        //                        }).ToList();
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        return null;
        //    }

        //}
    }
}
